home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16931 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: druid.borland.com!usenet
  2. From: "James J. Hartley" <jamesh@borland.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: HELP Borland C++ 5.0 pointer problem
  5. Date: Fri, 12 Apr 1996 12:47:20 -0700
  6. Organization: Borland International
  7. Message-ID: <316EB348.3220@borland.com>
  8. References: <4klke8$7mb@dub-news-svc-2.compuserve.com>
  9. NNTP-Posting-Host: yeti.borland.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.01 (Win95; I)
  14.  
  15. Andy Graham wrote:
  16. > Could you please tell me why the function below compiles and operates
  17. > correctly under win16 but not win32 ?
  18. >   char* test_function(char *in_data) {
  19. >         char buffer[20];
  20. >         strcpy(buffer, in_data);   // copy in_data to buffer
  21. >         in_data = buffer;   // set pointer to buffer
  22. >         return(in_data);
  23. >   }  // end test_function function
  24.  
  25. This is dangerous code under any platform.  The stack memory associated with 
  26. buffer will be available to any other function once the scope of test_function() 
  27. is exited.  I don't know the specifics of your application, but there are
  28. situations where bad coding is not brutally dealt with by the underlying OS;
  29. consider yourself lucky that this code worked under Win16 in the first place.
  30. One simple fix which may prove useful is to move buffer from the stack to the
  31. data segment(s) by declaring it static.
  32.  
  33. Jim
  34.